package com.jams.goodox.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


import com.jams.goodox.activity.mine.download.DownloadBean;

import java.util.ArrayList;
import java.util.List;


/**
 * @author HP
 */
public class DBManager {

    public volatile static DBManager mInstance;

    private Context mContext;
    private MySQLiteHelper mSqLiteHelper;

    public DBManager(Context context) {
        this.mContext = context;
        init();
    }

    public static DBManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (DBManager.class) {
                if (mInstance == null) {
                    mInstance = new DBManager(context);
                }
            }
        }
        return mInstance;
    }

    private void init() {
        mSqLiteHelper = new MySQLiteHelper(mContext);

    }

    public void insertDownList(List<DownloadBean> list) {
        if (list != null) {
            SQLiteDatabase db = mSqLiteHelper.openDb();
            db.beginTransaction();
            db.delete(MySQLiteHelper.TB_COUNTRY_LIST,null,null);
            for (DownloadBean info : list) {
                ContentValues mValues = new ContentValues();
                mValues.put(MySQLiteHelper.KEY_COUNTRY_LIST_NAME, info.getName()); //key $ value
                mValues.put(MySQLiteHelper.KEY_COUNTRY_LIST_PIC, info.getPic());
                mValues.put(MySQLiteHelper.KEY_COUNTRY_LIST_TITLE, info.getTitle());
                mValues.put(MySQLiteHelper.KEY_COUNTRY_LIST_URL, info.getUrl());
                db.insert(MySQLiteHelper.TB_COUNTRY_LIST, null, mValues);
            }

            db.setTransactionSuccessful();       //设置事务处理成功，不设置会自动回滚不提交
            db.endTransaction();       //处理完成
            mSqLiteHelper.closeDb();
        }
    }


    public void insertOneItem(DownloadBean info) {
        if (info != null) {
            boolean isBreak = false;
            SQLiteDatabase db = mSqLiteHelper.openDb();

            List<DownloadBean> queryDownList = queryDownList();
            Cursor cursor = db.query(MySQLiteHelper.TB_COUNTRY_LIST, null, null, null, null, null, null);
            List<DownloadBean> list = new ArrayList<>();
            while (cursor.moveToNext()) {
                if(info.getUrl().equals(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_COUNTRY_LIST_URL)))){
                    Log.d("DB","已存在的数据");
                    isBreak = true;
                    break;
                }else {
                    continue;
                }
            }
            cursor.close();

            if(isBreak){
                return;
            }
            Log.d("DB","插入新数据");
            //db.delete(MySQLiteHelper.TB_COUNTRY_LIST,null,null);
            db.beginTransaction();
            ContentValues mValues = new ContentValues();
            mValues.put(MySQLiteHelper.KEY_COUNTRY_LIST_NAME, info.getName()); //key $ value
            mValues.put(MySQLiteHelper.KEY_COUNTRY_LIST_PIC, info.getPic());
            mValues.put(MySQLiteHelper.KEY_COUNTRY_LIST_TITLE, info.getTitle());
            mValues.put(MySQLiteHelper.KEY_COUNTRY_LIST_URL, info.getUrl());
            db.insert(MySQLiteHelper.TB_COUNTRY_LIST, null, mValues);

            db.setTransactionSuccessful();       //设置事务处理成功，不设置会自动回滚不提交
            db.endTransaction();       //处理完成
            mSqLiteHelper.closeDb();
        }
    }


    public List<DownloadBean> queryDownList() {
        SQLiteDatabase db = mSqLiteHelper.openDb();

        Cursor cursor = db.query(MySQLiteHelper.TB_COUNTRY_LIST, null, null, null, null, null, null);

        List<DownloadBean> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            DownloadBean info = new DownloadBean();
            info.setName(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_COUNTRY_LIST_NAME)));
            info.setPic(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_COUNTRY_LIST_PIC)));
            info.setTitle(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_COUNTRY_LIST_TITLE)));
            info.setUrl(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_COUNTRY_LIST_URL)));
            list.add(info);
        }
        cursor.close();
        mSqLiteHelper.closeDb();
        return list;
    }

    public void update() {

    }

    public void delete() {

    }

}
